/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.sensor;

import wwcontrolserver.sensor.SensorRKey;
import wwcontrolserver.sensor.SensorKey;
import wwcontrolserver.sensor.AbstractSensor;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.collections.map.MultiValueMap;
import wwcontrolserver.WWControlServer;
//mport wwcontrolserver.AbstractSensor.SensorKey;
//import wwcontrolserver.AbstractSensor.SensorRKey;
/**
 *
 * @author Michael Ferry
 */
public class SensorContainer {
    //Maps a key to its corresponding values
    private final HashMap<SensorKey,AbstractSensor> SensorKeyMap = new HashMap<SensorKey,AbstractSensor>();
    //Organizes sensors based on their CalGroups
    private final MultiValueMap CalGroupMap = new MultiValueMap();
    //Organizes based on ifk and if the sensor is ratiometric or not
    private final MultiValueMap SensorRKeyMap = new MultiValueMap();
    //maps the sensors attached to each IFK;
    private final MultiValueMap SensorIFKMap = new MultiValueMap();
    
    public SensorContainer(AbstractSensor [] Sensors){
         for (int i = 0; i < Sensors.length; i++){
             SensorKeyMap.put(Sensors[i].GetKey(),Sensors[i]);
             CalGroupMap.put(Sensors[i].GetCalGroup(), Sensors[i].GetKey());
             SensorRKeyMap.put(Sensors[i].GetSensorRKey(),Sensors[i]);
             SensorIFKMap.put(Sensors[i].GetIFK(), Sensors[i]);
         }
         
//         int x=1;
//        SensorKeyMap.put(WWControlServer.P3_pH_KEY, WWControlServer.P3_pH_VALUE);
//        SensorKeyMap.put(WWControlServer.P3_ORP_KEY, WWControlServer.P3_ORP_VALUE);
//        SensorKeyMap.put(WWControlServer.P4_pH_KEY, WWControlServer.P4_pH_VALUE);
//        SensorKeyMap.put(WWControlServer.P4_ORP_KEY, WWControlServer.P4_ORP_VALUE);
//        SensorKeyMap.put(WWControlServer.P4_ORP_KEY, WWControlServer.P4_ORP_VALUE);        
    }
    
    public boolean IsKeyPartOfCalGroup(SensorKey KeyToTest, int CALGROUP) {
        if (SensorKeyMap.containsKey(KeyToTest)) {
            //AbstractSensor ValToTest = SensorKeyMap.get(KeyToTest);
            if (CALGROUP == SensorKeyMap.get(KeyToTest).GetCalGroup()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    public HashMap<Integer,AbstractSensor> GetHashMapOfSensorsAttachedToIFK(int ifkSerial){
        if(SensorIFKMap.containsKey(ifkSerial)){
            HashMap<Integer,AbstractSensor> AttachedSensorMap = new HashMap<Integer,AbstractSensor>();
            ArrayList<AbstractSensor> ListOfAttachedSensors;
            ListOfAttachedSensors = new ArrayList<AbstractSensor>(SensorIFKMap.getCollection(ifkSerial));
            for(AbstractSensor SensorToProcess: ListOfAttachedSensors){
                AttachedSensorMap.put(SensorToProcess.GetSNUM(), SensorToProcess);
            }
            return AttachedSensorMap;
        }else{
            return null;
        }
    }
    
    public int GetCalGroupOfSensor(SensorKey KeyToTest){
        AbstractSensor ValToTest = SensorKeyMap.get(KeyToTest);
        if(ValToTest != null){
            return ValToTest.GetCalGroup();          
        }else{
            return WWControlServer.CALGROUP_INDEPENDENT;
        }
        
    }
    public ArrayList<AbstractSensor> GetSensorsInCalGroup(int CalGroup){
        return (ArrayList<AbstractSensor>) CalGroupMap.getCollection(CalGroup);
    }
    //Returns null if there is no sensor matching the key
    public AbstractSensor GetSensor(SensorKey SKey){
        return SensorKeyMap.get(SKey);
    }
    public ArrayList<AbstractSensor> GetSensorsMatchingSRKey(SensorRKey SRKey){
        if(SensorRKeyMap.containsKey(SRKey)){
            return new ArrayList<AbstractSensor>(SensorRKeyMap.getCollection(SRKey));
        }
        else{
            return null;
        }
    }
}
